import {api, pt} from "./api"
import {pb} from "db://assets/script/network/ws/proto";
import {Long} from "protobufjs";

let instance = null
export class Player {

    model: pb.IPlayerModel
    constructor() {
        instance = this
    }

    static getInstance() {
        return instance
    }

    static destroyInstance() {
        instance = null
    }

    // 游客登录
    static loginByTourist(touristId: string, onSuccess: Function) {
        // 新建 player 实例
         instance = null
        new Player()
        api.playerLogin({touristId, loginType: pt.LoginType.Tourist}, onSuccess)
    }

    updatePlayerInfo(info: pb.IPlayerModel) {
        this.model = info
    }

    static shortId() {
        if (instance) {
            return instance.model.shortId
        }
        return 0
    }

    // 是否是本家
    static isSelf(shortId: number | Long) {
        return instance && shortId === instance.model.shortId
    }
}